/**
 * This file is part of aion-lightning <aion-lightning.org>.
 * 
 * aion-lightning is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * aion-lightning is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with aion-lightning.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionlightning.chatserver.service;

import java.util.Map;
import com.aionlightning.commons.utils.internal.chmv8.PlatformDependent;
import com.aionlightning.chatserver.model.ChatClient;
import com.aionlightning.chatserver.model.message.Message;
import com.aionlightning.chatserver.network.aion.serverpackets.SM_CHANNEL_MESSAGE;
import com.aionlightning.chatserver.network.netty.handler.ClientChannelHandler;

/**
 * @author ATracer
 */
public class BroadcastService
{
	private static BroadcastService instance = new BroadcastService();
	private Map<Integer, ChatClient> clients = PlatformDependent.newConcurrentHashMap();
	
	public static BroadcastService getInstance()
	{
		return instance;
	}

	/**
	 * @param client
	 */
	public void addClient(ChatClient client)
	{
		clients.put(client.getClientId(), client);
	}

	/**
	 * @param client
	 */
	public void removeClient(ChatClient client)
	{
		clients.remove(client.getClientId());
	}

	/**
	 * @param message
	 */
	public void broadcastMessage(Message message)
	{
		for (ChatClient client : clients.values())
		{
			if (client.isInChannel(message.getChannel()))
				sendMessage(client, message);
		}
	}

	/**
	 * @param chatClient
	 * @param message
	 */
	public void sendMessage(ChatClient chatClient, Message message)
	{
		ClientChannelHandler cch = chatClient.getChannelHandler();
		cch.sendPacket(new SM_CHANNEL_MESSAGE(message));
	}
}
